<?php

namespace app\dev\controller;

use think\Controller;
use turbo\area\TxArea;
use app\common\service\Estate;
use app\common\model\TxArea as TxAreaModel;


class Area extends Controller{

    public function index(){
        $res = TxArea::autoUpdate();

        var_dump($res);
    }

    
    public function province(){
        $res = TxArea::getTownsByProvince(410000);

        var_dump($res);
    }

    public function county(){
        $res = TxArea::getTownsByCity(320100);

        var_dump($res);
    }

    public function parse($lng,$lat)
    {
        $res = TxArea::parseLnglat($lng, $lat);

        var_dump($res);
    }

    public function getStreets(){
        $res = Estate::getStreesByArea([
            'lng' => 116.34799511512756,
            'lat' => 39.73037982425224,
        ],[
            'lng' => 116.33994848808288,
            'lat' => 39.72042824723185,
        ]);

        return json($res);
    }

    public function exportByProvince($areacode){
        $areacode = (string) $areacode;
        $res = [];
        if(preg_match("/^[\d]{2}0000$/",$areacode)){
            $sub = substr($areacode, 0, 2);
            $res['province'] = TxAreaModel::where('area_code', $areacode)->column("area_code,name");
            $res['city'] = TxAreaModel::where('level',2)->where('area_code','LIKE', "{$sub}%")->column("area_code,name");
            $res['county'] = TxAreaModel::where('level',3)->where('area_code','LIKE', "{$sub}%")->column("area_code,name");
            $res['town'] = TxAreaModel::where('level',4)->where('area_code','LIKE', "{$sub}%")->column("area_code,name");
        }

        return json($res);
    }
}